<?php
/**
 * # 路由规则
 * ## 验证带参数的路由规则
 * ## 必须完全匹配参数
 * Created by PhpStorm.
 * User: jiangjunxian
 * Date: 2016/12/1
 * Time: 13:20
 */

namespace app\modules\admin\components;


use yii\rbac\Item;
use yii\rbac\Rule;

class RouteRule extends Rule
{
    /* 默认Route的名称 */
    const RULE_NAME = 'route_rule';

    public $name = self::RULE_NAME;


    /**
     * Executes the rule.
     *
     * @param string|integer $user the user ID. This should be either an integer or a string representing
     * the unique identifier of a user. See [[\yii\web\User::id]].
     * @param Item $item the role or permission that this rule is associated with
     * @param array $params parameters passed to [[CheckAccessInterface::checkAccess()]].
     * @return boolean a value indicating whether the rule permits the auth item it is associated with.
     */
    public function execute($user, $item, $params)
    {
        $routeParams = isset($item->data['params']) ? $item->data['params'] : [];
        foreach ($routeParams as $key => $value) {
            if(!array_key_exists($key,$params) || $params[$key] != $value) {
                return false;
            }
        }

        return true;
    }
}